/**
 * 依赖全局变量 InfoGlobal
 * Created by shopnc on 2015/11/26.
 */
//定义表格
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true

    },
    {
        id: 'id',
        title: '编号',
        type: 'int',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastQuery: false
    },
    {
        id: 'c.name',
        title: '品种名称',
        type: 'string',
        headerClass: 'text-left width-80',
        columnClass: 'text-left width-80',
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'c.code',
        type: 'string',
        title: '品种代码',
        headerClass: 'text-left width-80',
        columnClass: 'text-left width-80',
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'contract',
        type: 'string',
        title: '主力交易合约',
        fastQuery: false
    },
    {
        id: 'nextContract',
        type: 'string',
        title: '下期主力交易合约',
        fastQuery: false
    },
    {
        id: 'pointValue',
        type: 'string',
        title: '每点合约价值',
        fastQuery: false
    },
    {
        id: 'wavePointMin',
        type: 'string',
        title: '最小波动点',
        fastQuery: false
    },
    {
        id: 'tradeNumber',
        type: 'string',
        title: '交易数量',
        fastQuery: false
    },
    {
        id: 'tradeTime',
        type: 'string',
        title: '交易时间限制',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'quoteTime',
        type: 'string',
        title: '行情时间',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'fee',
        type: 'string',
        title: '交易综合费',
        fastQuery: false
    },
    {
        id: 'open',
        type: 'string',
        title: '交易开关状态',
        fastQuery: false,
        codeTable: {
            0: $lang.tradeOpen.T0,
            1: $lang.tradeOpen.T1
        }
    },
    {
        id: 'exchangeRate',
        type: 'string',
        title: '汇率',
        fastQuery: false
    },
    {
        id: 'holiday',
        type: 'string',
        title: '节假日连接字符串',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'overnightFee',
        title: '过夜费',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false
    },
    {
        id: 'deposit',
        title: '保证金',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false
    },
    {
        id: 'everyCover',
        title: '每手清盘',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'openMax',
        title: '市价最大下单',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'openMin',
        title: '市价最小下单',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'limitMax',
        title: '限价最大下单',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'limitMin',
        title: '限价最小下单',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'overnigntDepositTimes',
        title: '隔夜保证金倍数',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false
    },
    {
        id: 'operation',
        title: '管理操作',
        type: 'string',
        columnClass: 'text-center width-100',
        fastSort: false,
        extra: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var html="",
                edit="",
                state = "";
            if ($("#commoRiskEdit") && $("#commoRiskEdit").val() == 1) {
                edit = "<a data-target='#editModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;编辑&nbsp;</a>";
            }
            html = edit  + state;
            return html;
        }
    }
];
var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + '/risk/json/list',
    exportFileName: '系统风控参数列表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    tools: 'refresh|faseQuery',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int: ["id","delFlag"], Timestamp: ["createTime"]},
    onGridComplete: function (grid) {
    }
};
var grid = $.fn.DtGrid.init(dtGridOption);
grid.sortParameter.columnId = ['asc_id'];

grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;
/**
 * 公告列表所使用的js
 */
var information = function () {

    /**
     * 刷新addmodal 上的图片
     */
    function _refreshImage() {
        $(".viewImage").jqthumb({
            width: 180,
            height: 100,
            after: function (imgObj) {
                imgObj.css('opacity', 0).animate({opacity: 1}, 1500);
            }
        });
    }


    //编辑器对象[添加]
    var _ueAdd;
    //编辑器对象[编辑]
    var _ueEdit;
    //删除URL
    var stateUrl = ncGlobal.adminRoot + "commo/json/state";

    /**
     * 删除
     */
    function _stateInfo(id,status, content) {
        var option='';
        if(status == "1"){
            option += '启用';
        }else{
            option += '禁用';
        }
        var tpl = '您选择对交易品种 <strong>' + content + '</strong>进行'+ option + '操作。<br/>您确定要进行该操作吗?'
        $.ncConfirm({
            url: stateUrl,
            data: {
                id: id,
                status:status
            },
            content: tpl,
            alertTitle: option+"操作"
        });
    }

    var addInfo = {
        initAddModal: function () {
            $addForm = $("#addForm");
            $addForm.find('input[name="name"]').val("");
            $addForm.find('input[name="code"]').val("");
            $(".addType").bootstrapSwitch('state', true);
            $addForm.find('input[name="sortNo"]').val("");
            $('#addFormPicPath').val("");
            $('#addFormPicImg').attr('src', ncGlobal.imgRoot+"default_image.gif");
            $("#contentAdd").val("");
            $(".alert-danger").remove();
            $addForm.psly().reset();
            _refreshImage();
        }
    }

    /**
     * 事件绑定
     * @private
     */
    function _bindEvent() {
        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters['lk_name'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });

        // bycj [ 编辑对话框显示时调用 ]
        $('#editModal').on('show.bs.modal', function (event) {
            var    //获取接受事件的元素
                button = $(event.relatedTarget),
                //获取data 参数
                datano = button.data('no'),
                modal = $(this),
                //获取列表框中的原始数据
                gridData = grid.sortOriginalDatas[datano],
                editForm = $("#editForm");
                modal.find('input[name="id"]').val(gridData.id);
                modal.find('input[name="commoId"]').val(gridData.commoId);
                modal.find('input[name="contract"]').val(gridData.contract);
                modal.find('input[name="nextContract"]').val(gridData.nextContract);
                modal.find('input[name="nextContractTime"]').val(gridData.nextContractTime);
                modal.find('input[name="pointValue"]').val(gridData.pointValue);
                modal.find('input[name="wavePointMin"]').val(gridData.wavePointMin);
                modal.find('input[name="tradeNumber"]').val(gridData.tradeNumber);
                modal.find('input[name="tradeTime"]').val(gridData.tradeTime);
                modal.find('input[name="quoteTime"]').val(gridData.quoteTime);
                modal.find('input[name="profitStopTimes"]').val(gridData.profitStopTimes);
                modal.find('input[name="loseStopPoint"]').val(gridData.loseStopPoint);
                modal.find('input[name="loseStopRate"]').val(gridData.loseStopRate);
                modal.find('input[name="fee"]').val(gridData.fee);
                modal.find('input[name="upStop"]').val(gridData.upStop);
                modal.find('input[name="downStop"]').val(gridData.downStop);
                modal.find('input[name="open"]').val(gridData.open);
                modal.find('input[name="exchangeRate"]').val(gridData.exchangeRate);
                modal.find('input[name="allotRate"]').val(gridData.allotRate);
                modal.find('input[name="holiday"]').val(gridData.holiday);
                modal.find('input[name="followFee"]').val(gridData.followFee);
                modal.find('input[name="followAllotRate"]').val(gridData.followAllotRate);
                modal.find('input[name="exchType"]').val(gridData.exchType);
                modal.find('input[name="overnightFee"]').val(gridData.overnightFee);
                modal.find('input[name="deposit"]').val(gridData.deposit);
                modal.find('input[name="everyCover"]').val(gridData.everyCover);
                modal.find('input[name="openMax"]').val(gridData.openMax);
                modal.find('input[name="openMin"]').val(gridData.openMin);
                modal.find('input[name="limitMax"]').val(gridData.limitMax);
                modal.find('input[name="limitMin"]').val(gridData.limitMin);
                modal.find('input[name="clearValue"]').val(gridData.clearValue);
                modal.find('input[name="overnigntDepositTimes"]').val(gridData.overnigntDepositTimes);
                //清除错误提示
                editForm.psly().reset();
                $(".alert-danger").remove()
                _refreshImage();
        });


    }

    //外部可调用
    return {
        init: function () {
            _bindEvent();
        },
        stateInfo: _stateInfo,
    }
}()
$(function () {
    grid.load();
    information.init();
})